﻿using EfCoreDemo.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace EfCoreDemo.Data
{
    public class DemoContext : DbContext
    {
        //构造函数传入参数
        public DemoContext(DbContextOptions options) :base (options)
        {

        }

        //配置方式
        //protected override void OnConfiguring(DbContextOptionsBuilder dbContextOptionsBuilder)
        //{
        //    dbContextOptionsBuilder
        //        .UseLoggerFactory(ConsoleLoggerFacotry)
        //        .EnableSensitiveDataLogging()
        //        .UseSqlServer("Server=.;uid=sa;pwd=shris2020;database=EfCoreDemo;");
        //}

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //使用 fluant api 设置表 GamePlayer 的联合主键
            modelBuilder.Entity<GamePlayer>().HasKey(x => new { x.GameId, x.PlayerId });
            //制定1：1 关系表的主表,这里 HasForeignKey<Resume>(x=>x.PlayerId) 确定 Resume 是主表
            modelBuilder.Entity<Resume>().
                HasOne(x => x.Player).
                WithOne(x => x.Resume).
                HasForeignKey<Resume>(x => x.PlayerId);

            //modelBuilder.Entity<UserTest>().Property(x=>x.ResumeId).HasDefaultValue(Guid.NewGuid());

            modelBuilder.Entity<UserTest>(u =>
            {
                u.HasOne(x => x.Resume).WithOne(x => x.User).HasForeignKey<UserResume>(x => x.UserId);
                //u.HasIndex(x => x.ResumeId).IsUnique().HasFilter<UserTest>($"[{nameof(UserTest.ResumeId)}]<>00000000-0000-0000-0000-000000000000");

            });

            modelBuilder.Entity<UserResume>().HasOne(x => x.Certification).WithOne(x => x.Resume).HasForeignKey<Certification>(x => x.ResumeId);

            modelBuilder.Entity<Certification>(c =>
            {
                c.HasQueryFilter(x => !x.Deleted);  //增加查询过滤条件 delete=true 的不要查出来
                c.HasIndex(x => x.ResumeId).IsUnique().HasFilter($"[{nameof(Certification.Deleted)}]=0"); //指定唯一性的过滤条件是delete=0
            });


            //modelBuilder.Entity<UserResume>()
            //    .HasOne(x => x.User)
            //    .WithOne(y => y.Resume) //指定 UserResume  和 UserTest 的 1：1 关系
            //    .HasForeignKey<UserResume>(z => z.UserId).OnDelete(DeleteBehavior.Restrict); //指定UserTest为主表，且可空

            modelBuilder.Entity<Note>(e =>
            {
                e.HasOne(x => x.Item).WithOne(x => x.Note).HasForeignKey<Note>(x => x.ItemId);
                e.HasQueryFilter(x => !x.Deleted);
                e.HasIndex(x => x.ItemId).IsUnique().HasFilter($"[{nameof(Note.Deleted)}]=0");
            });

            //没有主键的表 或者 视图，需要额外申明
            modelBuilder.Entity<PlayerClub>().HasNoKey()  //如果在此处停住，EF 会为 PlayerClub创建一张表，但实际是一个视图，这不是我想要的
                .ToView("PlayserClubView");

        }
        public DbSet<Club> Clubs { get; set; }

        public DbSet<League> Leagues { get; set; }

        public DbSet<Player> Players { get; set; }

        public DbSet<Resume> Resumes { get; set; }

        public DbSet<Game> Games { get; set; }

        public DbSet<PlayerClub> PlayerClubs { get; set; }


        public DbSet<UserTest> UserTests { get; set; }

        public DbSet<ProjectTest> ProjectTests { get; set; }

        public DbSet<UserProject> UserProjects { get; set; }

        public DbSet<Certification> Certifications { get; set; }

        public DbSet<Item> Items { get; set; }

        public DbSet<Note> Notes { get; set; }


        public DbSet<UserResume> UserResumes { get; set; }


        public static readonly ILoggerFactory ConsoleLoggerFacotry =
            LoggerFactory.Create(builder => 
            {
                builder.AddFilter((categroy, level) =>
                categroy == DbLoggerCategory.Database.Command.Name &&
                    level == LogLevel.Information).AddConsole();
            
            });
    }
}
